package com.jas.logic;

import java.util.ArrayList;
import java.util.TreeMap;

import com.jas.common.CommandFormatException;
import com.jas.common.DateFormatException;
import com.jas.common.Event;
import com.jas.common.EventDateException;
import com.jas.common.Log;
import com.jas.common.Notification;
import com.jas.logic.Logic.KeywordType;
import com.jas.storage.StorageException;

public class LogicAPI {

	private Logic logic;
	private KeywordAndParameterParser keywordAndParameterParser;

	//@author a0118677r
	public LogicAPI(Logic logic) {
		this.logic = logic;
		keywordAndParameterParser = new KeywordAndParameterParser();
	}

	//@author a0118677r
	public void updateLanguage() {
		keywordAndParameterParser.updateLanguage();
	}

	//@author a0118677r
	/**
	 * This method breaks down the parameters passed and decides which type of
	 * event that should be added
	 * 
	 * @param addParameterString
	 *            User's entered add parameters
	 * @throws CommandFormatException
	 * @throws StorageException
	 * @throws DateFormatException
	 * @throws EventDateException
	 */
	public ArrayList<Event> add(String addParameterString)
			throws CommandFormatException, StorageException,
			DateFormatException, EventDateException {

		TreeMap<KeywordType, String> mappingOfKeywordTypesToParameters = keywordAndParameterParser
				.mapKeywordsToParameters(addParameterString);

		// for Logging the file only
		Log.addLog("Add: " + addParameterString);
		// Log finish

		return logic.add(addParameterString,mappingOfKeywordTypesToParameters);
	}

	//@author a0118677r
	/**
	 * This method breaks down the parameters passed and decides which display
	 * method to call
	 * 
	 * @param displayParameterString
	 *            User's entered display parameters
	 * @throws CommandFormatException
	 * @throws DateFormatException
	 */
	public ArrayList<Event> display(String displayParameterString)
			throws CommandFormatException, DateFormatException {
		TreeMap<KeywordType, String> mappingOfKeywordTypesToParameters = keywordAndParameterParser
				.mapKeywordsToParameters(displayParameterString);

		// for Logging the file + and action to ActionList
		Log.addLog("Display: " + displayParameterString);
		// end log

		return logic.display(mappingOfKeywordTypesToParameters);
	}

	//@author a0118677r
	/**
	 * This method calls the logic component <code>delete()</code> method to
	 * delete events listed by the user.
	 * 
	 * @param deleteParameterString
	 *            User's entered delete parameters
	 * @throws StorageException
	 * @throws EventIdException
	 */
	public ArrayList<Event> delete(String deleteParameterString)
			throws StorageException, EventIdException {
		// for logging only
		Log.addLog("Delete: " + deleteParameterString);
		// end log

		if (deleteParameterString.toLowerCase().trim().equals("all")) {
			return logic.deleteAll();
		} else {
			return logic.delete(deleteParameterString);
		}
	}

	// @author A0098803W
	/**
	 * This method deletes the last entered notification.
	 * 
	 * @param notificationObj
	 *            The <code>Notification</code> object to delete.
	 * @throws StorageException
	 */
	public Notification delete(Notification notificationObj)
			throws StorageException {
		return logic.delete(notificationObj);

	}

	//@author a0118677r
	public ArrayList<Event> update(String eventId, String updateParameterString)
			throws StorageException, DateFormatException, EventDateException {

		TreeMap<KeywordType, String> mappingOfKeywordTypesToParameters = keywordAndParameterParser
				.mapKeywordsToParameters(updateParameterString);

		// for logging only
		Log.addLog("Update: " + eventId + "," + updateParameterString);
		// end log

		return logic.update(eventId, mappingOfKeywordTypesToParameters);
	}

	//@author A0098803W
	/**
	 * This method calls the <code>search</code> method in the
	 * logic component.
	 * 
	 * @param searchText
	 *            The search text entered by the user.
	 * @return List of events that matches the search text or an empty list if
	 *         no such event is found.
	 * @throws DateFormatException
	 */
	public ArrayList<Event> search(String searchText)
			throws DateFormatException {
		ArrayList<Event> matchingList = logic.search(searchText);
		return matchingList;

	}

	//@author A0121310J
	// undo the event by tracing the log file document
	public void undo() throws StorageException, DateFormatException,
			EventDateException, EventIdException

	{
		logic.undo();

	}

	//@author A0098803W
	/**
	 * This method adds a new notification to the notification storage file.
	 * @param date User input date
	 * @param dateFormat User's date format.
	 * @param message The notification message.
	 * @return The newly added <code>Notification</code> object.
	 * @throws CommandFormatException
	 * @throws StorageException
	 * @throws DateFormatException
	 */
	public Notification add(String date, String dateFormat, String message)
			throws CommandFormatException, StorageException,
			DateFormatException {
		return logic.add(date, dateFormat, message);
	}
	
	//@author A0098803W
	/**
	 * This method updates the messaging gateway for the program.
	 * @param url The URL for the gateway.
	 * @throws CommandFormatException 
	 * @throws StorageException 
	 */
	public void updateGateway(String url) throws CommandFormatException, StorageException{
		logic.updateGateway(url);
	}

}